﻿using M.Warehouse.API.Application.Command.WarehousesCommand;
using M.Warehouse.Domain;
using M.Warehouse.Domain.DTO;
using M_ApiResult;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace M.Warehouse.API.Controllers
{
    /// <summary>
    /// 仓库控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WarehouseController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="mediator"></param>
        public WarehouseController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 添加仓库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Create(WarehousesCreateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data=await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message= ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改仓库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Update(WarehousesUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> Delete([FromQuery]WarehousesDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批量删除仓库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> BatchDel([FromQuery]WarehousesBatchDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示仓库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<Warehouses>>> Show([FromQuery]WarehousesShowCommand command)
        {
            ApiResult<PageDTO<Warehouses>> result = new ApiResult<PageDTO<Warehouses>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 返填仓库
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Warehouses>> Backfill([FromQuery] WarehousesBackfillCommand command)
        {
            ApiResult<Warehouses> result = new ApiResult<Warehouses>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 仓库平面图显示
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Warehouses>>> WarehousePlan([FromQuery] WarehousesPlanShowCommand command)
        {
            ApiResult<List<Warehouses>> result = new ApiResult<List<Warehouses>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 仓库连表反向查询
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<WarehousesReverseDto>> WarehousesReverse([FromQuery] WarehousesReverseById command)
        {
            ApiResult<WarehousesReverseDto> result = new ApiResult<WarehousesReverseDto>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
